package com.example.review;

import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.review.Fragment.AlertDialogFragment;
import com.example.review.Fragment.CameraFragment;
import com.example.review.Fragment.DwonReFragment;
import com.example.review.Fragment.ExpandFragment;
import com.example.review.Fragment.GridViewFragment;
import com.example.review.Fragment.IntentFragment;
import com.example.review.Fragment.LitepalFragment;
import com.example.review.Fragment.PotoFragment;
import com.example.review.Fragment.PvFragment;
import com.example.review.Fragment.SortFragment;
import com.example.review.Fragment.TimeFragment;
import com.example.review.Fragment.VarPageFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.toobar)
    Toolbar toobar;
    @BindView(R.id.home_page)
    FrameLayout home_page;
    @BindView(R.id.home_list)
    ListView home_list;
    @BindView(R.id.life_menu)
    FrameLayout leftMenu;
    @BindView(R.id.drawerLayout)
    DrawerLayout drawerLayout;

    private FragmentManager fm = getSupportFragmentManager();
    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Window window = getWindow();
        window.setFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        initView();
        initData();
    }

    private void initData() {
        final String[] data = {"排序", "时间", "二级listView", "AlertDialog对话框", "旅游信息", "Litepal", "视频加载","图片加载  ", "Vapage", "跳转","时间选择器","拍照"};
        final List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new SortFragment());
        fragmentList.add(new TimeFragment());
        fragmentList.add(new ExpandFragment());
        fragmentList.add(new AlertDialogFragment());
        fragmentList.add(new GridViewFragment());
        fragmentList.add(new LitepalFragment());
        fragmentList.add(new PvFragment());
        fragmentList.add(new PotoFragment());
        fragmentList.add(new VarPageFragment());
        fragmentList.add(new IntentFragment());
        fragmentList.add(new DwonReFragment());
        fragmentList.add(new CameraFragment());
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.support_simple_spinner_dropdown_item, data);
        home_list.setAdapter(arrayAdapter);
        home_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                ft = fm.beginTransaction();
                ft.replace(R.id.home_page, fragmentList.get(i));
                toobar.setTitle(data[i]);
                drawerLayout.closeDrawers();
                ft.commit();
            }
        });
    }

    private void initView() {
        toobar = (Toolbar) findViewById(R.id.toobar);
        home_page = (FrameLayout) findViewById(R.id.home_page);
        home_list = (ListView) findViewById(R.id.home_list);
        leftMenu = (FrameLayout) findViewById(R.id.life_menu);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        //获取侧边栏默认宽度
        ViewGroup.LayoutParams leftParams = leftMenu.getLayoutParams();
        //获取屏幕宽度
        final int width = metrics.widthPixels;
        //获取屏幕高度
        final int height = metrics.heightPixels;
        //设置侧边的宽高(如果不重新设置，即时设置match_parent也只能占屏幕百分80)
        leftParams.width = width;
        leftMenu.setLayoutParams(leftParams);
        //另外openDrawerContentDescRes 打开图片   closeDrawerContentDescRes 关闭图片
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, toobar, 0, 0);
        //初始化状态
        drawerLayout.addDrawerListener(toggle);
        toggle.syncState();

        //设置背景蒙层颜色
        drawerLayout.setScrimColor(Color.TRANSPARENT);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerStateChanged(int newState) {
                //侧边栏状态
            }

            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                //获取主页内容view
                View mContent = drawerLayout.getChildAt(0);
                //获取侧边栏内容 view
                View mMenu = drawerView;
                //主页面移动
                drawerLayout.getChildAt(0).setTranslationX(width * slideOffset);

            }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {

            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {

            }
        });
    }


}
